﻿using CMS.Models;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace CMS.ViewModel
{
    public class InputCargoViewModel : ViewModelBase
    {
        //public InputCargoViewModel()
        //{
        //    Cargos = new CargoProvider().Select();
        //}
        

        /// <summary>
        /// 当前物资
        /// </summary>
        public Cargo Cargo { get; set; }
        public AppData AppData { get; set; } = AppData.Instance;

        private Record record = new Record();
        /// <summary>
        /// 入库流水单
        /// </summary>
        public Record Record
        {
            get { return record; }
            set
            {
                record = value;
                RaisePropertyChanged();
            }
        }


        private List<Cargo> cargos = new List<Cargo>();
        /// <summary>
        /// 所有物资
        /// </summary>
        public List<Cargo> Cargos
        {
            get { return cargos; }
            set
            {
                cargos = value;
                RaisePropertyChanged();
            }
        }


        /// <summary>
        /// 入库
        /// </summary>
        public RelayCommand<Window> AddRecordCommand
        {
            get
            {
                return new RelayCommand<Window>((arg) =>
                {
                    //if (string.IsNullOrEmpty(record.CargoName)) return;
                    //var cargo = this.Cargos.FirstOrDefault(item => item.Name == record.CargoName);
                    if (Cargo == null)   return;
                    record.CargoId = Cargo.Id;
                    record.CargoName = Cargo.Name;
                    if (0 == record.Number) return;
                    record.MemberId = AppData.CurrentUser.Id;
                    record.MemberName = AppData.CurrentUser.Name;
                    record.InsertDate = DateTime.Now;
                    record.RecordType = true;//入库

                    var count = new RecordProvider().Insert(record);
                    if (count == 0)
                    {
                        MessageBox.Show("添加失败");
                    }
                    else
                    {
                        arg.Close();
                    }
                });
            }
        }

        /// <summary>
        /// 关闭窗体
        /// </summary>
        public RelayCommand<Window> CancelCommand
        {
            //get { return cancelCommand; }
            get
            {
                return new RelayCommand<Window>((arg) =>
                {
                    if (arg == null) return;
                    // Here you would typically clear the login form or close the login window
                    //MessageBox.Show("Login cancelled.");
                    arg.Close();
                });
            }
        }


    }
}
